package doan.chuyennganh.model;

import java.util.ArrayList;

public class AttendanceList {
	private ArrayList<Attendance> list;
	public AttendanceList() {
		list = new ArrayList<Attendance>();
	}
	public void add(Attendance  attendance) {
		if(!list.contains(attendance)){
			list.add(attendance);
		}
	}
	public Attendance get(int index) {
		return list.get(index);
	}
	public void remove(int index) {
		if(index < list.size())
			list.remove(index);
	}
	public int size() {
		return list.size();
	}
	public Attendance getLastAttendance() {
		if(list.size() == 0){
			return null;
		}
		return list.get(list.size() - 1);
	}
	public Attendance getAttendanceByDate(String date){
		for(Attendance att : list){
			if(att.getDate().toString().equalsIgnoreCase(date)){
				return att;
			}
		}
		return null;
	}
	public ArrayList<String> absenceOverRate(int totalPeriod, int maxRate) {
		if(list == null || list.size() <= 0) return null;
		ArrayList<String> res = new ArrayList<String>();
		ArrayList<String> keys = list.get(0).getAllKey();
		for(String key : keys){
			int period = 0;
			for (int k = 0; k < list.size(); k++) {
				Attendance tmp = list.get(k);
				period += tmp.get(key);
			}
			
				int currentRate = (int) Math
						.round((double) period / totalPeriod * 100);
				if(currentRate >= maxRate){
					res.add(key);
			
			}
		}
		return res;
	}
}
